﻿<?php
$whmcs_path = "whmcs";
$per_page = 50;

if (!isset($_SESSION["adminid"]) || (!mysql_num_rows(mysql_query("SELECT permid FROM tbladmins JOIN tbladminperms ON tbladmins.roleid = tbladminperms.roleid WHERE permid=9 AND tbladmins.id=". intval($_SESSION["adminid"])))))
{
	exit("Access Denied");
}
?>
<style type="text/css">
.ext-el-mask{z-index:20000;position:fixed;top:0;left:0;-moz-opacity:0.5;opacity:.50;filter:alpha(opacity=50);background-color:#CCC;width:100%;height:100%;zoom:1;}
.ext-el-mask-msg{z-index:20001;position:absolute;top:0;left:0;border:1px solid #6593cf;background:#c3daf9 url(images/tb-blue.gif) repeat-x 0 -16px;padding:2px;}
.ext-el-mask-msg div{padding:5px 10px 5px 10px;background:#eee;border:1px solid #a3bad9;color:#333;font:normal 12px tahoma,arial,helvetica,sans-serif;cursor:wait;}
.x-mask-loading div{padding:5px 10px 5px 25px;background:#eee url(images/loading.gif) no-repeat 5px 5px;line-height:16px;}
#loading-msg{position:fixed;top:45%!important;left:43%!important;z-index:20001!important;}
.tab{padding-top:2px;padding-bottom:2px;padding-left:10px;padding-right:10px;background-color:#efefef;border:1px solid #cccccc;cursor:pointer;cursor:hand;}
.tab a{text-decoration:none;color:#000;}
.tabselected{padding-top:2px;padding-bottom:2px;padding-left:10px;padding-right:10px;	background-color:#FFF;border-top:1px solid #cccccc;border-left:1px solid #CCC;	border-right:1px solid #cccccc;border-bottom:0px;font-weight:bold;cursor:pointer;cursor:hand;}
.tabbox{padding:10px;border-left:1px solid #CCC;border-right:1px solid #CCC;	border-bottom:1px solid #CCC;}
.tabspacer{border-bottom:1px solid #CCC;}
.admindatatable{background-color:#CCC;}
tr.admindatatableheader td{background-color:#EEE;}
.admindatatableheader td{font-weight:bold;text-align:center;background-color:#EEE;}
.admindatatable td{background-color: #FFF;}
.admindatatablehighlight td{background-color:#EEF4FB;}
</style>
<div id="loading-mask" class="ext-el-mask" style="display: none;"></div>
<div id="loading-msg" class="ext-el-mask-msg x-mask-loading" style="display: none;">
  <div>Processing...</div>
</div>
<script src='jquery.js'></script>
<script>
function approve(id)
{
  $("#loading-mask").css('display', '');      
  $("#loading-msg").css('display', '');  
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
  else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	xmlhttp.onreadystatechange=function()
	{		
		if(xmlhttp.readyState==4)
		{
      $("#loading-mask").css('display', 'none');
      $("#loading-msg").css('display', 'none');    
			if (xmlhttp.responseText=="1")
      {
        document.getElementById('stat'+id).innerHTML="<img src='images/statusok.gif'/>";
        document.getElementById('stat'+id).style.color="#0F6";
        document.getElementById('abtn'+id).style.display='none';
        document.getElementById('sbtn'+id).style.display='none';
        document.getElementById('vrow'+id).style.display='none';
      }
      else
      {        
        alert("Error approving client! Error code #"+xmlhttp.responseText);
      }
		}
	}
	var url="https://<?php echo $_SERVER["HTTP_HOST"]."/".$whmcs_path;?>/modules/admin/client_tracker/ajax.php";
	url=url+"?op=approve&id="+id;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function revoke(id)
{  
  $("#loading-mask").css('display', '');      
  $("#loading-msg").css('display', '');  
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
  else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	xmlhttp.onreadystatechange=function()
	{		
		if(xmlhttp.readyState==4)
		{
      $("#loading-mask").css('display', 'none');
      $("#loading-msg").css('display', 'none');    
			if (xmlhttp.responseText=="1")
      {
        document.getElementById('stat'+id).innerHTML="Pending";
        document.getElementById('stat'+id).style.color="#F00";
        document.getElementById('rbtn'+id).style.display="none";
        document.getElementById('sbtn'+id).style.display="";
      }
      else
      {        
        alert("Error revoking client! Error code #"+xmlhttp.responseText);
      }
		}
	}
	var url="https://<?php echo $_SERVER["HTTP_HOST"]."/".$whmcs_path;?>/modules/admin/client_tracker/ajax.php";
	url=url+"?op=revoke&id="+id;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function showVinfo(id)
{
  document.getElementById('vrow'+id).style.display='';
}

function notify(id)
{ 
	$("#loading-mask").css('display', '');      
  $("#loading-msg").css('display', '');  
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
  else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	xmlhttp.onreadystatechange=function()
	{		
		if(xmlhttp.readyState==4)
		{
      $("#loading-mask").css('display', 'none');
      $("#loading-msg").css('display', 'none');    
			if (xmlhttp.responseText=="1")
      {        
        document.getElementById('sbtn'+id).value = "Notice Sent!";
        document.getElementById('sbtn'+id).disabled = true;
      }
		}
	}
	var url="https://<?php echo $_SERVER["HTTP_HOST"]."/".$whmcs_path;?>/modules/admin/client_tracker/ajax.php";
	url=url+"?op=notify&id="+id;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);  
}
</script>
<?php
function not_completed($client)
{
  return ((trim($client["firstname"]." ".$client["lastname"])=="") || 
          (trim($client["email"])=="") ||
          (trim($client["address1"]." ".$client["address2"])=="") ||
          (trim($client["phonenumber"])==""));
}

function not_matched($client)
{
  return ($client["firstname"]!=$client["vfirstname"] ||
          $client["lastname"]!=$client["vlastname"] ||
          $client["email"]!=$client["vemail"] ||
          $client["address1"]!=$client["vaddress1"] ||
          $client["address2"]!=$client["vaddress2"] ||
          $client["city"]!=$client["vcity"] ||
          $client["country"]!=$client["vcountry"] ||
          $client["phonenumber"]!=$client["vphonenumber"]);
}

if (isset($_REQUEST["page"]))
  $page = intval($_REQUEST["page"]);
else
  $page = 1;
  
$param = "";
$start = ($page-1)*$per_page;
$cond = "1";
  
if (isset($_REQUEST["status"]) && ($_REQUEST["status"]=="1"))
{
  $cond .= " AND tblclients.status='Active'";
  $param .= "&status=".intval($_REQUEST["status"]);
  $status_select = "<option value=''>Any</option><option value='1' selected>Active</option><option value='2'>Inactive</option><option value='3'>Closed</option>";
}
else if (isset($_REQUEST["status"]) && ($_REQUEST["status"]=="2"))
{
  $cond .= " AND tblclients.status='Inactive'";
  $param .= "&status=".intval($_REQUEST["status"]);
  $status_select = "<option value=''>Any</option><option value='1'>Active</option><option value='2' selected>Inactive</option><option value='3'>Closed</option>";
}
else if (isset($_REQUEST["status"]) && ($_REQUEST["status"]=="3"))
{
  $cond .= " AND tblclients.status='Closed'";
  $param .= "&status=".intval($_REQUEST["status"]);
  $status_select = "<option value=''>Any</option><option value='1'>Active</option><option value='2'>Inactive</option><option value='3' selected>Closed</option>";
}
else
  $status_select = "<option value='' selected>Any</option><option value='1'>Active</option><option value='2'>Inactive</option><option value='3'>Closed</option>";
  
if (isset($_REQUEST["verified"]) && ($_REQUEST["verified"]=="1"))
{
  $cond .= " AND (mod_cinfo.approved = 1)";
  $param .= "&verified=1";
  $verified_select = "<option value=''>Any</option><option value='1' selected>Yes</option><option value='2'>No</option>";
}
else if (isset($_REQUEST["verified"]) && ($_REQUEST["verified"]=="2"))
{
  $cond .= " AND ((mod_cinfo.approved != 1) OR (mod_cinfo.approved is NULL))";
  $param .= "&verified=2";
  $verified_select = "<option value=''>Any</option><option value='1'>Yes</option><option value='2' selected>No</option>";
}
else
  $verified_select = "<option value='' selected>Any</option><option value='1'>Yes</option><option value='2'>No</option>";

if (isset($_REQUEST["email"]) && ($_REQUEST["email"]!=""))
{
  $cond .= " AND (tblclients.email LIKE '%".mysql_real_escape_string($_REQUEST["email"])."%')";
  $param .= "&email=".$_REQUEST["email"];
}

if (isset($_REQUEST["name"]) && ($_REQUEST["name"]!=""))
{
  $cond .= " AND ((tblclients.firstname LIKE '%".mysql_real_escape_string($_REQUEST["name"])."%') OR (tblclients.lastname LIKE '%".mysql_real_escape_string($_REQUEST["name"])."%'))";
  $param .= "&name=".$_REQUEST["name"];
}

?>
<form action="<? echo $modulelink.$param;?>" method="POST"> 
<table width=100% cellspacing=0> 
<tr>
  <td width=5 class="tabspacer"><img src="images/spacer.gif" width=5 height=1></td> 
  <td id="tab0" class="tab" onClick="TabClick(0)">Filter</td> 
  <td width=100% class="tabspacer">&nbsp;</td> 
</tr> 
<tr id="tabbox0">
  <td colspan=3 class="tabbox" align='center'>
    <table cellspacing=0 border=0><tr>
    <td style='padding-right:5px;'>Client Name: <input name="name" value="<?php echo $_REQUEST["name"];?>"></td>
    <td style='padding-right:5px;'>Email: <input name="email" value="<?php echo $_REQUEST["email"];?>"></td>
    <td style='padding-right:5px;'>Status: <select name="status"><?php echo $status_select;?></select></td>
    <td style='padding-right:5px;'>Verified: <select name="verified"><?php echo $verified_select;?></select></td>
    <td><input type="submit" value="Show" class="button"></td>
    </tr></table>
  </td></tr></table> 
<script language="javascript"> 
  var tabs = 1;
  function TabClick(sel) 
  {
    for(i=0;i<tabs;i++) 
    {
      document.getElementById('tab'+i).className='tab';
      document.getElementById('tabbox'+i).style.display='none';
    }
    document.getElementById('tab'+sel).className='tabselected';
    document.getElementById('tabbox'+sel).style.display='';
  }
  for(i=0;i<tabs;i++) 
  {
    document.getElementById('tabbox'+i).style.display='none';
  }
</script>
</form><br/>

<?php
  
$r = mysql_query("SELECT tblclients.id as id, tblclients.firstname as firstname, tblclients.lastname as lastname, tblclients.email as email, tblclients.phonenumber as phonenumber, tblclients.status as status, tblclients.address1 as address1, tblclients.address2 as address2, tblclients.city as city, tblclients.country as country, mod_cinfo.firstname as vfirstname, mod_cinfo.lastname as vlastname, mod_cinfo.email as vemail, mod_cinfo.phonenumber as vphonenumber, mod_cinfo.address1 as vaddress1, mod_cinfo.address2 as vaddress2, mod_cinfo.city as vcity, mod_cinfo.country as vcountry, mod_cinfo.approved as approved, mod_cinfo.approvaldate as approvaldate, mod_cinfo.noticecount as noticecount, mod_cinfo.lastnotice as lastnotice, DATEDIFF(CURDATE(), mod_cinfo.approvaldate) as adays, DATEDIFF(CURDATE(), mod_cinfo.lastnotice) as ndays FROM tblclients LEFT JOIN mod_cinfo ON tblclients.id = mod_cinfo.id WHERE (".$cond.") ORDER BY tblclients.id ASC LIMIT ".$start.", ".$per_page.";");

$total = mysql_num_rows(mysql_query("SELECT tblclients.id FROM tblclients LEFT JOIN mod_cinfo ON tblclients.id = mod_cinfo.id WHERE (".$cond.");"));

if ($total > 0)
{
  echo "<table width='100%'><tr><td>".$total." Records Found, Page ".$page." of ".ceil($total/$per_page)."</td><form method='post' action='".$modulelink.$param."'><td><div align='right'>Jump to Page: <select name='page' onchange='submit();'>";
  for ($i=0;$i<ceil($total/$per_page);$i++)
  {
    echo "<option value='".($i+1)."' ";
    if ($page==($i+1)) 
      echo "selected";
    echo ">".($i+1);
  }
  echo "</select><input type='submit' value='Go'></div></td></form></tr></table>
  <table width='100%' cellspacing='1' class='admindatatable'>
  <tr class='admindatatableheader'>
    <td>ID</td>
    <td>Name</td>
    <td>Email</td>
    <td>Phone</td>
    <td>Address</td>
    <td>Status</td>
    <td>Approval</td>
    <td>Action</td>
  </tr>";
  while ($client = mysql_fetch_assoc($r))
  {
    echo "<tr>
        <td style='padding:5px;text-align:center;'><a href='clientssummary.php?userid=".$client["id"]."' target='_blank'>".$client["id"]."</a></td>
        <td style='padding:5px;'><a href='clientssummary.php?userid=".$client["id"]."' target='_blank'>".$client["firstname"]." ".$client["lastname"]."</a></td>
        <td style='padding:5px;'>".$client["email"]."</td>
        <td style='padding:5px;'>".$client["phonenumber"]."</td>
        <td style='padding:5px;'>".$client["address1"].$client["address2"]." ".$client["city"]." ".$client["country"]."</td>
        <td style='padding:5px;text-align:center;'>".$client["status"]."</td>";
      
    if ($client["approved"]!=NULL) 
    {
      if ($client["approved"]==1)        
      {
        echo "<td><div id='stat".$client["id"]."' style='color:#0F6;text-align:center;'><img src='images/statusok.gif' title='Verified at ".$client["approvaldate"]."'/></div></td>
        <td style='text-align:center;'><input type='submit' value='Revoke' onclick='revoke(".$client["id"].");' id='rbtn".$client["id"]."' /><input type='submit' value='Send Notice' onclick='notify(".$client["id"].");' id='sbtn".$client["id"]."' style='display:none;' /></td>
        </tr>";
      }
      else
      {
        echo "<td><div id='stat".$client["id"]."' style='color:#F00;text-align:center;'>Pending ";
        if (not_matched($client))
          echo "<a href='javascript:showVinfo(".$client["id"].");' title='View verified record'><img src='images/info.gif' style='border:0px;valign:middle;'/></a>";
        echo "</div></td>
      <td style='text-align:center;'>";
        if (!not_completed($client))
          echo "<input type='submit' value='Approve' onclick='approve(".$client["id"].");' id='abtn".$client["id"]."' />";
        echo " <input type='submit'"; 
        
        if (isset($client["noticecount"]) && ($client["noticecount"]==1))
        {          
          if ($client["ndays"]==0)
            echo "value='Notice #1 sent today' disabled";
          else if ($client["ndays"]<7)
            echo "value='Notice #1 sent ".$client["ndays"]." days ago' disabled";
          else
            echo "value='Send Notice #2'";
        }
        elseif (isset($client["noticecount"]) && ($client["noticecount"]==2))
        {
          if ($client["ndays"]==0)
            echo "value='Notice #2 sent today' disabled";
          else if ($client["ndays"]<7)
            echo "value='Notice #2 sent ".$client["ndays"]." days ago' disabled";
          else
            echo "value='Send Notice #3'";
        }
        elseif (isset($client["noticecount"]) && ($client["noticecount"]==3))
        {
          if ($client["ndays"]==0)
            echo "value='Notice #3 sent today' disabled";
          else if ($client["ndays"]<3)
            echo "value='Notice #3 sent ".$client["ndays"]." days ago' disabled";
          else
            echo "value='Suspend Account' disabled";
        }
        else
          echo "value='Send Notice'";
        echo " onclick='notify(".$client["id"].");' id='sbtn".$client["id"]."'/></td>
          </tr>";
        if (not_matched($client))
        {    
          echo "<tr id='vrow".$client["id"]."' style='display:none;'>
            <td style='padding:5px;text-align:center;background:#CFF;'>".$client["id"]."</td>          
            <td style='padding:5px;background:#CFF;'>".$client["vfirstname"]." ".$client["vlastname"]."</td>
            <td style='padding:5px;background:#CFF;'>".$client["vemail"]."</td>
            <td style='padding:5px;background:#CFF;'>".$client["vphonenumber"]."</td>
            <td style='padding:5px;background:#CFF;'>".$client["vaddress1"].$client["vaddress2"]." ".$client["vcity"]." ".$client["vcountry"]."</td>          
            <td colspan=3 style='padding:5px;background:#CFF;'>Last approved: ".$client["approvaldate"];
            if ($client["adays"]) 
              echo " (".$client["adays"]." days ago)";
            echo "</td>
            </tr>";
        }
      }
    }
    else
    {
      echo "<td><div id='stat".$client["id"]."' style='color:#930;text-align:center;'>New</div></td>
        <td style='text-align:center;'>
          <input type='submit' value='Approve' onclick='approve(".$client["id"].");' id='abtn".$client["id"]."'";
      if (not_completed($client))
        echo " style='display:none;'";
      echo "/> <input type='submit' value='Send Notice' onclick='notify(".$client["id"].");' id='sbtn".$client["id"]."'/></td>
        </tr>";
    }      
  }
  echo "</table><br/>
  <p align=center>";
  if ($page>1)
    echo "<a href='".$modulelink.$param."&page=".($page-1)."'><< Previous Page</a>";
  else
    echo "<< Previous Page";
  echo "&nbsp;&nbsp;";
  if ($page<ceil($total/$per_page))
    echo "<a href='".$modulelink.$param."&page=".($page+1)."'>Next Page >></a>";
  else
    echo "Next Page >>";
  echo "</p>";
}
else
  echo "<center>Found Nothing!</center>";
?>